<?php echo $this->element('displayHeader'); ?>
<?php echo $ajax->form('SRH', 'post', array('update'=>'content', 'indicator' => 'indicator', 'url' => array('action' => 'save')));?>
<div class="toolbar">
    <div id="tLeft">
        <?php if ($mode == 'add') : ?>
            <?php echo $this->element('toolbar', array('buttons' => array('save', 'clear', 'back'))); ?>
        <?php else :  // mode edit ?>
            <?php echo $this->element('toolbar', array('buttons' => array('add', 'save', array('print' => array('model' => 'SRH')), 'back'))); ?>
        <?php endif; ?>
    </div>
    <div id="tRight"><?php echo $this->element('neighbors', array('model' => 'SRH', 'title' => 'id')); ?></div>
</div>

<?php if ($mode == 'add' && $crhs == null) :  // ถ้ายังไม่มีใบ dispatch ส่งมาถึงลูกค้า ?>
<script type="text/javascript">
    $j(function() {
        alert("There is no returned product form customer.");
        $j('.save').hide();
    });
</script>
<?php endif; ?>

<?php echo $form->input('SRH.id'); ?>
<?php echo $form->hidden('mode', array('value' => $mode)); ?>
<fieldset>
    <table>
        <tr>
            <td width="180px"><?php echo $form->label('SRH.retrieve_no', 'Retrieve no'); ?></td>
            <td width="300px">
                <?php if ($mode == 'edit') : ?>
                <span id="retrieve_no"><?php echo $this->data['SRH']['retrieve_no'] ?></span>
                <?php endif; ?>
                &nbsp;
            </td>
            <td width="180px"><?php echo $form->label('SRH.cust_return_to_supplier_head_id', 'Customer Return no.', array('class' => 'required')); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->select('SRH.cust_return_to_supplier_head_id', $crhs, $this->data['CRtSH']['id'], array('tabindex' => 5)); ?>
                <?php else : ?>
                <span id="return_no"><?php echo $this->data['CRtSH']['return_no'] ?></span>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('SRH.retrieve_date', 'Retrieve date'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $html->tag('span', $today); ?>
                    <?php echo $form->hidden('SRH.retrieve_date', array('id' => 'retrieve_date', 'value' => $today)); ?>
                <?php else : ?>
                <span id="retrieve_date"><?php echo $this->data['SRH']['retrieve_date'] ?></span>
                <?php endif; ?>
            </td>
            <td><?php echo $form->label('SRH.customer_id', 'Customer'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->hidden('SRH.customer_id', array('value' => $this->data['CRtSH']['customer_id'])); ?>
                <span id="SRHCustomerName"><?php if (!empty($this->data)) echo $this->data['Customer']['name']; ?></span>
                <?php else : ?>
                    <?php echo $this->data['Customer']['name'] ?>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('SRH.retrieve_by', 'Retrieve by'); ?></td>
            <td>
                <?php if($mode == 'add') : ?>
                    <?php echo $html->tag('span', $retrieve_by); ?>
                    <?php echo $form->hidden('SRH.retrieve_by', array('value' => $retrieve_by)); ?>
                <?php else : ?>
                    <?php echo $html->tag('span', $this->data['SRH']['retrieve_by']); ?>
                <?php endif; ?>
            </td>
            <td><?php echo $form->label('SRH.site_id', 'Site'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->hidden('SRH.site_id', array('value' => $this->data['CRtSH']['site_id'])); ?>
                <span id="SRHSiteName"><?php if (!empty($this->data)) echo $this->data['Site']['name']; ?></span>
                <?php else : ?>
                    <?php echo $this->data['Site']['name'] ?>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('SRH.retrieve_status_id', 'Status', array('class' => 'required')); ?></td>
            <td colspan="3">
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->select('SRH.retrieve_status_id', $retrieveStatuses); ?>
                    <span id="divRS" class="caption">All products will return back to customer inventory automatically.</span>
                <?php else : ?>
                    <?php echo $this->data['RS']['title'] ?>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('CRtSH.vendor_document_no', 'Vendor Document no.'); ?></td>
            <td colspan="3">
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->text('SRH.vendor_document_no'); ?>
                <?php else : ?>
                    <?php echo !empty($this->data['CRtSH']['vendor_document_no'])? $this->data['CRtSH']['vendor_document_no']: '-' ?>
                <?php endif; ?>
            </td>
        </tr>
    </table>
</fieldset>

<fieldset>
    <table class="data">
        <thead>
            <tr>
                <th class="no">No</th>
                <th width="130px">Part no.</th>
                <th width="130px">Stock Code</th>
                <th>Description</th>
                <th width="100px">Retrieve Qty</th>
                <th width="50px">Unit</th>
                <th width="100px">Price</th>
                <th width="100px">Amount</th>
            </tr>
        </thead>
        <?php if (empty($this->data)) : ?>
        <tbody id="productRow">
            <tr class="odd-cell">
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </tbody>
        <?php elseif ($mode == 'add') : $count = count($this->data['CRtS'])?>
        <tbody id="tbodySR">
                <?php $j = 0;
                for($i = 0; $i < $count; ++$i) : ?>
                    <?php $ps_count = count($this->data['CRtS'][$i]['ProductSerial']);
        for($k = 0; $k < $ps_count; ++$k) : ?>
            <tr class="<?php echo (($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'; ?>">
                <td><span><?php echo $j + 1; ?></span></td>
                <td>
                                <?php echo $form->input("SR.{$j}.id"); ?>
                                <?php echo $form->hidden("SR.{$j}.product_id", array('value' => $this->data['CRtS'][$i]['product_id'])); ?>
                                <?php echo $form->hidden("SR.{$j}.cust_product_site_id", array('value' => $this->data['CRtS'][$i]['cust_product_site_id'])); ?>
                                <?php echo $form->hidden("SR.{$j}.retrieve_qty", array('value' => $this->data['CRtS'][$i]['ProductSerial'][$k]['return_qty'])); ?>
                                <?php echo $form->hidden("SR.{$j}.cust_return_to_supplier_id", array('value' => $this->data['CRtS'][$i]['id'])); ?>
                                <?php echo $form->hidden("SR.{$j}.price", array('value' => $this->data['CRtS'][$i]['ProductSerial'][$k]['price'])); ?>
                                <?php echo $html->link($this->data['CRtS'][$i]['Product']['supplier_part_no'],
                                '/supplier_consignment_lists/view/' . $this->data['CRtS'][$i]['product_id'] . '/' . strtotime('now'),
            array('target' => '_blank')); ?>
                </td>
                <td><?php echo $this->data['CRtS'][$i]['CustProductSite']['stock_code']; ?></td>
                <td><?php echo $this->data['CRtS'][$i]['Product']['title']; ?></td>
                <td class="text-right"><?php echo $this->data['CRtS'][$i]['ProductSerial'][$k]['return_qty']; ?></td>
                <td class="center"><?php echo $this->data['CRtS'][$i]['Product']['Unit']['title']; ?></td>
                <td class="text-right"><?php echo number_format($this->data['CRtS'][$i]['ProductSerial'][$k]['price'], 2); ?></td>
                <td class="text-right"><?php echo number_format($this->data['CRtS'][$i]['return_qty'] * $this->data['CRtS'][$i]['ProductSerial'][$k]['price'], 2); ?></td>
            </tr>
                        <?php ++$j;
        endfor; ?>
            <?php endfor; ?>
        </tbody>
            <?php else : $count = count($this->data['SR']) // mode edit?>
        <tbody>
    <?php for($i = 0; $i < $count; ++$i) : ?>
            <tr class="<?php echo (($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'; ?>">
                <td><span><?php echo $i + 1; ?></span></td>
                <td>
                            <?php echo $html->link($this->data['SR'][$i]['Product']['supplier_part_no'],
        '/supplier_consignment_lists/view/' . $this->data['SR'][$i]['product_id'] . '/' . strtotime('now'),
        array('target' => '_blank')); ?>
                </td>
                <td><?php echo $this->data['SR'][$i]['CustProductSite']['stock_code']; ?></td>
                <td><?php echo $this->data['SR'][$i]['Product']['title']; ?></td>
                <td class="text-right"><?php echo $this->data['SR'][$i]['retrieve_qty']; ?></td>
                <td class="center"><?php echo $this->data['SR'][$i]['Product']['Unit']['title']; ?></td>
                <td class="text-right"><?php echo number_format($this->data['SR'][$i]['price'], 2); ?></td>
                <td class="text-right"><?php echo number_format($this->data['SR'][$i]['price'] * $this->data['SR'][$i]['retrieve_qty'], 2); ?></td>
            </tr>
            <?php endfor; ?>
        </tbody>
<?php endif; ?>
    </table>
</fieldset>
<fieldset>
    <table>
        <tr>
            <td width="50px" class="top"><?php echo $form->label('SRH.remark'); ?></td>
            <td><?php echo $form->textarea('SRH.remark', array('cols' => 80, 'rows' => 3)); ?></td>
        </tr>
    </table>
</fieldset>
<?php echo $form->end(); ?>

<?php if ($mode == 'add') : ?>
<script type="text/javascript">
    $j(function() {
        $j('#SRHCustReturnToSupplierHeadId').bind('change', function(event) {
            requestUpdater('content', 'supplier_retrieves/view/cust_return_to_supplier_head_id:' + $j(this).val());
        });

        /**
         * แสดง/ซ่อน caption ตอน decline return
         */
        $j('#SRHRetrieveStatusId').bind('click', function(event) {
            // deline
            if ($j(this).val() == 2) {
                $j('#divRS').show();
            } else {
                $j('#divRS').hide();
            }
        }).click();

        $j('#SRHId').parent('form').beforeSubmit(function() {
        if ($j.areEmpty('SRHCustReturnToSupplierHeadId', 'SRHRetrieveStatusId')) {
            return false;
        }
    });
    });
</script>
<?php endif; ?>

<?php //debug($this->data); ?>